import { reqGoodsInfo, reqAddOrUpdateShopCart } from "@/api";

import { getUUID } from '@/utils/uuid_token';

const state = {
  goodInfo: {},
  //游客临时身份
  uuid_token: getUUID()
};
const mutations = {
  GETGOODINFO(state, goodInfo) {
    state.goodInfo = goodInfo;
  }
};
const actions = {
  //获取产品信息
  async getGoodInfo({ commit }, skuId) {
    let result = await reqGoodsInfo(skuId)

    if (result.code === 200) {
      commit('GETGOODINFO', result.data);
    }
  },
  //将产品加到购物车
  async addOrUpdateShowCart({ commit }, { skuId, skuNum }) {
    let result = await reqAddOrUpdateShopCart(skuId, skuNum);
    // console.log(result);
    if (result.code === 200) {
      return "ok";//返回任意非空字符串都代表成功
    } else {
      return Promise.reject(new Error('faile'))
    }

  }
};
const getters = {
  categoryView(state) {
    return state.goodInfo.categoryView || {}
  },
  skuInfo(state) {
    return state.goodInfo.skuInfo || {}
  },
  spuSaleAttrList(state) {
    return state.goodInfo.spuSaleAttrList || []
  }
};
export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}